var role = {
	init : function() {
		role.roleGrid = $('#roleGrid');
		
		role.roleAddForm = $('#roleAddForm');
		role.menuAdd = $('#menuAdd');
		role.roleAddDialog = $('#roleAddDialog');
		
		role.roleUpdateForm = $('#roleUpdateForm');
		role.menuUpdate = $('#menuUpdate');
		role.roleUpdateDialog = $('#roleUpdateDialog');
		
		role.rolePower = $("#rolePower");
		role.reRolePower = $("#reRolePower");
		
		role.roleGrid.datagrid({
		    url: window.ctx + '/sys/role/listRole',
		    fitColumns: true,
		    striped: true,
		    rownumbers: true,
		    pagination: true,
		    pageSize: 10,
		    pageList: [5, 10, 15, 20, 25],
		    pageNumber: 1,
		    //sortName: 'name',
		    //sortOrder: 'desc',
		    toolbar: '#roleToolbar',
		    columns:[[
				{field:'id', width:100, checkbox: true},
				{field:'name', title:'角色名', width:20, align:'center'},
				{field:'stateName', title:'状态', width: 60, align:'center'},
				{field:'state', title:'状态值', width: 60, align:'center', hidden: 'true'}
		    ]],
			onDblClickRow: function(rowIndex, rowData) {
				role.toolbar.showUpdateDialog(rowData.id);
			}
		});
		(function(){
			document.onkeydown = function()  
		    {  
				var alt = event.altKey;
				var keyCode = window.event.keyCode;
		        if (alt && keyCode==65 ) {  
		        	role.toolbar.showAddDialog();
		        } else if(alt && keyCode==67){
		        	role.toolbar.showUpdateDialog();
		        }else if(alt && keyCode==86){
		        	role.toolbar.changeState();
		        }else if(alt && keyCode==90){
		        	role.toolbar.redo();
		        }else if(alt && keyCode==83){
		        	 role.toolbar.searchrole();
		        }
		    }  
		})();
	},
	toolbar: {
		showAddDialog: function() {
			role.roleAddDialog.dialog('open');
			$("#name").textbox('textbox').focus();
			$("#name").textbox('textbox').attr("placeholder", "输入角色名，该字段不能重复");
			role.rolePower.combotree({
				url: ctx + '/sys/menu/getPowerCombotree',
                valueField: 'id',
                textField: 'text',
                multiple:true,
                required: true,
                editable: false,
                onClick: function (node) { 
                },
                onLoadSuccess: function (node, data) {
                	role.rolePower.combotree('tree').tree("collapseAll"); 
                }
			});
		},
		hiddenAddDialog: function() {
			role.roleAddDialog.dialog('close');
			role.toolbar.resetAddForm();
		},
		resetAddForm: function() {
			role.roleAddForm.form('reset');
		},
		add: function() {
			if (role.roleAddForm.form('validate')) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(ctx + '/sys/role/addRole', role.roleAddForm.serialize(), function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '新增成功！',
							timeout: 3000,
							showType: 'slide'
						});
						//role.toolbar.resetAddFrom();
						role.roleGrid.datagrid('reload');
					} else {
						$.messager.alert('系统提示', '新增失败！', 'error')
					}
					$.messager.progress('close');
					role.toolbar.hiddenAddDialog();
				});
			}
		},
		showUpdateDialog: function(id) {
			var getData = function(v) {
				$.post(ctx + '/sys/role/showRoleInfo', {"id" : v}, function(data) {
					if (data) {
						role.roleUpdateForm.form('load', {
							"id": data.id,
							"name": data.name,
							"state": data.state,
						});
						
						$('#roleUpdateDialog').dialog('open');
						role.reRolePower.combotree({
							url: ctx + '/sys/menu/getPowerCombotree/' + v,
			                valueField: 'id',
			                textField: 'text',
			                multiple:true,
			                required: true,
			                editable: false,
			                onClick: function (node) { 
			                },
			                onLoadSuccess: function (node, data) {
			                	var result = [];
			                	for(var i = 0; i < data.length; i ++) {
			                		if(data[i].check) {
			                			result.push(data[i])
			                		}
			                		var child = data[i].children;
			                		for(var j = 0; j < child.length; j++) {
			                			if(child[j].check) {
			                				result.push(child[j]);
			                			}
			                		}
			                	}
			                	role.reRolePower.combotree('setValues', result);
			                	role.reRolePower.combotree('tree').tree("collapseAll"); 
			                }
						});
						
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');
			}
			if (id) {
				getData(id);
			} else {
				var row = role.roleGrid.datagrid('getSelections');
				if (row.length == 1) {
					getData(row[0].id);
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			}
		},
		changeState: function(row) {
			var getData = function(v) {
				$.messager.confirm('系统提示', '你确认要更改状态吗？该操作有一定的风险，请谨慎考虑', function(result){
					if (result){
						$.messager.progress({
							text : '正在处理...'
						});
						$.post(ctx + '/sys/role/updateRoleState', {"id" : v.id, "state" : v.state}, function(data) {
							if (data) {
								$.messager.show({
									title: '系统提示',
									msg: '修改成功！',
									timeout: 3000,
									showType: 'slide'
								});
								role.roleGrid.datagrid('reload'); 
							} else {
								$.messager.alert('系统提示', '修改失败！', 'error');
							}
							$.messager.progress('close');
						});
					} else {
						$.messager.alert('系统提示', '你取消了状态修改！', 'info')
					}
				});
			}
			if (row) {
				getData(row);
			} else {
				var row = role.roleGrid.datagrid('getSelections');
				if (row.length == 1) {
					getData(row[0]);
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			}
		},
		hiddenUpdateDialog: function() {
			$('#roleUpdateDialog').dialog('close');
			role.toolbar.resetUpdateFrom();
		},
		resetUpdateFrom: function() {
			role.roleUpdateForm.form('reset');
		},
		update: function() {
			if (role.roleUpdateForm.form('validate')) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(ctx + '/sys/role/updateRole', $("#roleUpdateForm").serialize(), function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						// role.toolbar.resetUpdateFrom();
						role.roleGrid.datagrid('reload');
					} else {
						$.messager.alert('系统提示', '修改失败！', 'error');
					}
					$.messager.progress('close');
					role.toolbar.hiddenUpdateDialog();
				});
			}
		},
		remove: function() {
			var row = role.roleGrid.datagrid('getSelections');
			if (row.length > 0) {
				var ids = [];
				$.each(row, function(i, v) {
					ids.push(v.id);
				});
				$.post(ctx + '/sys/role/deleteRole', {ids: ids.join(',')}, function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '删除成功！',
							timeout: 3000,
							showType: 'slide'
						});
						role.roleGrid.datagrid('reload');
					} else {
						$.messager.alert('系统提示', '删除失败！', 'error')
					}
					$.messager.progress('close');
				});
			} else {
				$.messager.show({
					title: '系统提示',
					msg: '请选择数据进行删除！',
					timeout: 3000,
					showType: 'slide'
				});	
			}
		},
		redo: function() {
			role.roleGrid.datagrid('unselectAll');
		},
		searchrole: function() {
			var roleStateGroup = $("#roleStateGroup").combobox("getValue");
			var searchRoleName = $("#searchRoleName").textbox("getValue");
			role.roleGrid.datagrid({
			    url: ctx + '/sys/role/searchByCondition',
			    queryParams: {
			    	"searchName": searchRoleName,
			    	"state": roleStateGroup
			    },
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 10,
			    pageList: [5, 10, 15, 20, 25],
			    pageNumber: 1,
			    //sortName: 'name',
			    //sortOrder: 'desc',
			    toolbar: '#roleToolbar',
			    columns:[[
			    	{field:'id', width:100, checkbox: true},
					{field:'name', title:'角色名', width:20, align:'center'},
					{field:'stateName', title:'状态', width: 60, align:'center'},
					{field:'state', title:'状态值', width: 60, align:'center', hidden: 'true'}
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					role.toolbar.showUpdateDialog(rowData.id);
				}
			});
		}
	},
	row : {
		
	}
};

$(function(){
	role.init();
	$("#stateGroup").combobox({
		valueField:'name',
        textField:'value',
		width: 70,			
		panelHeight: 'auto',
		url: window.ctx + '/sys/staticValue/listEnableStaticValueCombobox',
		onLoadSuccess: function () { 
			var data = $('#stateGroup').combobox('getData');
		    if (data.length > 0) {
		    	$('#stateGroup').combobox('select', data[0].name);
		    } 
		}
	})
});